home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / java / io / RandomAccessFile.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  37.4 KB  |  989 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)RandomAccessFile.java    1.50 98/09/24
  3.  *
  4.  * Copyright 1994-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.io;
  16.  
  17.  
  18. /**
  19.  * Instances of this class support both reading and writing to a 
  20.  * random access file. A random access file behaves like a large 
  21.  * array of bytes stored in the file system. There is a kind of cursor, 
  22.  * or index into the implied array, called the <em>file pointer</em>; 
  23.  * input operations read bytes starting at the file pointer and advance 
  24.  * the file pointer past the bytes read. If the random access file is 
  25.  * created in read/write mode, then output operations are also available; 
  26.  * output operations write bytes starting at the file pointer and advance 
  27.  * the file pointer past the bytes written. Output operations that write 
  28.  * past the current end of the implied array cause the array to be 
  29.  * extended. The file pointer can be read by the 
  30.  * <code>getFilePointer</code> method and set by the <code>seek</code> 
  31.  * method. 
  32.  * <p>
  33.  * It is generally true of all the reading routines in this class that 
  34.  * if end-of-file is reached before the desired number of bytes has been 
  35.  * read, an <code>EOFException</code> (which is a kind of 
  36.  * <code>IOException</code>) is thrown. If any byte cannot be read for 
  37.  * any reason other than end-of-file, an <code>IOException</code> other 
  38.  * than <code>EOFException</code> is thrown. In particular, an 
  39.  * <code>IOException</code> may be thrown if the stream has been closed.
  40.  *
  41.  * @author  unascribed
  42.  * @version 1.50, 09/24/98
  43.  * @since   JDK1.0
  44.  */
  45.  
  46. public class RandomAccessFile implements DataOutput, DataInput {
  47.     private FileDescriptor fd;
  48.  
  49.     /**
  50.      * Creates a random access file stream to read from, and optionally 
  51.      * to write to, a file with the specified name. A new 
  52.      * {@link FileDescriptor} object is created to represent the 
  53.      * connection to the file.
  54.      * <p>
  55.      * The mode argument must either be equal to <code>"r"</code> or 
  56.      * <code>"rw"</code>, indicating that the file is to be opened for 
  57.      * input only or for both input and output, respectively. The 
  58.      * write methods on this object will always throw an 
  59.      * <code>IOException</code> if the file is opened with a mode of 
  60.      * <code>"r"</code>. If the mode is <code>"rw"</code> and the 
  61.      * file does not exist, then an attempt is made to create it.
  62.      * An <code>IOException</code> is thrown if the name argument
  63.      * refers to a directory.
  64.      * <p>
  65.      * If there is a security manager, its <code>checkRead</code> method
  66.      * is called with the <code>name</code> argument
  67.      * as its argument to see if read access to the file is allowed.
  68.      * If the mode is "rw", the security manager's
  69.      * <code>checkWrite</code> method
  70.      * is also called with the <code>name</code> argument
  71.      * as its argument to see if write access to the file is allowed.
  72.      *
  73.      * @param      name   the system-dependent filename.
  74.      * @param      mode   the access mode.
  75.      * @exception  IllegalArgumentException  if the mode argument is not equal
  76.      *               to <code>"r"</code> or to <code>"rw"</code>.
  77.      * @exception  FileNotFoundException  if the file exists but is a directory
  78.      *                   rather than a regular file, or cannot be opened or
  79.      *                   created for any other reason
  80.      * @exception  SecurityException         if a security manager exists and its
  81.      *               <code>checkRead</code> method denies read access to the file
  82.      *               or the mode is "rw" and the security manager's
  83.      *               <code>checkWrite</code> method denies write access to the file.
  84.      * @see        java.lang.SecurityException
  85.      * @see        java.lang.SecurityManager#checkRead(java.lang.String)
  86.      * @see        java.lang.SecurityManager#checkWrite(java.lang.String)
  87.      */
  88.     public RandomAccessFile(String name, String mode)
  89.     throws FileNotFoundException
  90.     {
  91.     boolean rw = mode.equals("rw");
  92.     if (!rw && !mode.equals("r"))
  93.         throw new IllegalArgumentException("mode must be r or rw");
  94.     SecurityManager security = System.getSecurityManager();
  95.     if (security != null) {
  96.         security.checkRead(name);
  97.         if (rw) {
  98.         security.checkWrite(name);
  99.         }
  100.     }
  101.     fd = new FileDescriptor();
  102.     open(name, rw);
  103.     }
  104.  
  105.     /**
  106.      * Creates a random access file stream to read from, and optionally 
  107.      * to write to, the file specified by the <code>File</code> argument. 
  108.      * A new {@link FileDescriptor} object is created to represent 
  109.      * this file connection. 
  110.      * <p>
  111.      * The mode argument must either be equal to <code>"r"</code> or 
  112.      * <code>"rw"</code>, indicating that the file is to be opened for 
  113.      * input only or for both input and output, respectively. The 
  114.      * write methods on this object will always throw an 
  115.      * <code>IOException</code> if the file is opened with a mode of 
  116.      * <code>"r"</code>. If the mode is <code>"rw"</code> and the 
  117.      * file does not exist, then an attempt is made to create it.
  118.      * An <code>IOException</code> is thrown if the file argument
  119.      * refers to a directory.
  120.      * <p>
  121.      * If there is a security manager, its <code>checkRead</code> method
  122.      * is called with the pathname of the <code>file</code>
  123.      * argument as its argument to see if read access to the file is allowed.
  124.      * If the mode is "rw", the security manager's
  125.      * <code>checkWrite</code> method
  126.      * is also called with the path argument
  127.      * to see if write access to the file is allowed.
  128.      *
  129.      * @param      file   the file object.
  130.      * @param      mode   the access mode.
  131.      * @exception  IllegalArgumentException  if the mode argument is not equal
  132.      *               to <code>"r"</code> or to <code>"rw"</code>.
  133.      * @exception  FileNotFoundException  if the file exists but is a directory
  134.      *                   rather than a regular file, or cannot be opened or
  135.      *                   created for any other reason
  136.      * @exception  SecurityException         if a security manager exists and its
  137.      *               <code>checkRead</code> method denies read access to the file
  138.      *               or the mode is "rw" and the security manager's
  139.      *               <code>checkWrite</code> method denies write access to the file.
  140.      * @see        java.io.File#getPath()
  141.      * @see        java.lang.SecurityManager#checkRead(java.lang.String)
  142.      * @see        java.lang.SecurityManager#checkWrite(java.lang.String)
  143.      */
  144.     public RandomAccessFile(File file, String mode) throws IOException {
  145.     this(file.getPath(), mode);
  146.     }
  147.  
  148.     /**
  149.      * Returns the opaque file descriptor object associated with this stream.
  150.      *
  151.      * @return     the file descriptor object associated with this stream.
  152.      * @exception  IOException  if an I/O error occurs.
  153.      * @see        java.io.FileDescriptor
  154.      */
  155.     public final FileDescriptor getFD() throws IOException {
  156.     if (fd != null) return fd;
  157.     throw new IOException();
  158.     }
  159.  
  160.     /**
  161.      * Opens a file and returns the file descriptor.  The file is 
  162.      * opened in read-write mode if writeable is true, else 
  163.      * the file is opened as read-only.
  164.      * If the <code>name</code> refers to a directory, an IOException
  165.      * is thrown.
  166.      *
  167.      * @param name the name of the file
  168.      * @param writeable the boolean indicating whether file is 
  169.      * writeable or not.
  170.      */
  171.     private native void open(String name, boolean writeable)
  172.     throws FileNotFoundException;
  173.  
  174.     // 'Read' primitives
  175.  
  176.     /**
  177.      * Reads a byte of data from this file. The byte is returned as an 
  178.      * integer in the range 0 to 255 (<code>0x00-0x0ff</code>). This 
  179.      * method blocks if no input is yet available. 
  180.      * <p>
  181.      * Although <code>RandomAccessFile</code> is not a subclass of 
  182.      * <code>InputStream</code>, this method behaves in exactly the same 
  183.      * way as the {@link InputStream#read()} method of 
  184.      * <code>InputStream</code>.
  185.      *
  186.      * @return     the next byte of data, or <code>-1</code> if the end of the
  187.      *             file has been reached.
  188.      * @exception  IOException  if an I/O error occurs. Not thrown if  
  189.      *                          end-of-file has been reached.
  190.      */
  191.     public native int read() throws IOException;
  192.  
  193.     /**
  194.      * Reads a sub array as a sequence of bytes. 
  195.      * @param b the data to be written
  196.      * @param off the start offset in the data
  197.      * @param len the number of bytes that are written
  198.      * @exception IOException If an I/O error has occurred.
  199.      */
  200.     private native int readBytes(byte b[], int off, int len) throws IOException;
  201.  
  202.     /**
  203.      * Reads up to <code>len</code> bytes of data from this file into an 
  204.      * array of bytes. This method blocks until at least one byte of input 
  205.      * is available. 
  206.      * <p>
  207.      * Although <code>RandomAccessFile</code> is not a subclass of 
  208.      * <code>InputStream</code>, this method behaves in the exactly the 
  209.      * same way as the {@link InputStream#read(byte[], int, int)} method of 
  210.      * <code>InputStream</code>.
  211.      *
  212.      * @param      b     the buffer into which the data is read.
  213.      * @param      off   the start offset of the data.
  214.      * @param      len   the maximum number of bytes read.
  215.      * @return     the total number of bytes read into the buffer, or
  216.      *             <code>-1</code> if there is no more data because the end of
  217.      *             the file has been reached.
  218.      * @exception  IOException  if an I/O error occurs.
  219.      */
  220.     public int read(byte b[], int off, int len) throws IOException {
  221.     return readBytes(b, off, len);
  222.     }
  223.  
  224.     /**
  225.      * Reads up to <code>b.length</code> bytes of data from this file 
  226.      * into an array of bytes. This method blocks until at least one byte 
  227.      * of input is available. 
  228.      * <p>
  229.      * Although <code>RandomAccessFile</code> is not a subclass of 
  230.      * <code>InputStream</code>, this method behaves in the exactly the 
  231.      * same way as the {@link InputStream#read(byte[])} method of 
  232.      * <code>InputStream</code>.
  233.      *
  234.      * @param      b   the buffer into which the data is read.
  235.      * @return     the total number of bytes read into the buffer, or
  236.      *             <code>-1</code> if there is no more data because the end of
  237.      *             this file has been reached.
  238.      * @exception  IOException  if an I/O error occurs.
  239.      */
  240.     public int read(byte b[]) throws IOException {
  241.     return readBytes(b, 0, b.length);
  242.     }
  243.  
  244.     /**
  245.      * Reads <code>b.length</code> bytes from this file into the byte 
  246.      * array, starting at the current file pointer. This method reads 
  247.      * repeatedly from the file until the requested number of bytes are 
  248.      * read. This method blocks until the requested number of bytes are 
  249.      * read, the end of the stream is detected, or an exception is thrown. 
  250.      *
  251.      * @param      b   the buffer into which the data is read.
  252.      * @exception  EOFException  if this file reaches the end before reading
  253.      *               all the bytes.
  254.      * @exception  IOException   if an I/O error occurs.       
  255.      */
  256.     public final void readFully(byte b[]) throws IOException {
  257.     readFully(b, 0, b.length);
  258.     }
  259.  
  260.     /**
  261.      * Reads exactly <code>len</code> bytes from this file into the byte 
  262.      * array, starting at the current file pointer. This method reads 
  263.      * repeatedly from the file until the requested number of bytes are 
  264.      * read. This method blocks until the requested number of bytes are 
  265.      * read, the end of the stream is detected, or an exception is thrown. 
  266.      *
  267.      * @param      b     the buffer into which the data is read.
  268.      * @param      off   the start offset of the data.
  269.      * @param      len   the number of bytes to read.
  270.      * @exception  EOFException  if this file reaches the end before reading
  271.      *               all the bytes.
  272.      * @exception  IOException   if an I/O error occurs.
  273.      */
  274.     public final void readFully(byte b[], int off, int len) throws IOException {
  275.         int n = 0;
  276.     do {
  277.         int count = this.read(b, off + n, len - n);
  278.         if (count < 0)
  279.         throw new EOFException();
  280.         n += count;
  281.     } while (n < len);
  282.     }
  283.  
  284.     /**
  285.      * Attempts to skip over <code>n</code> bytes of input discarding the 
  286.      * skipped bytes. 
  287.      * <p>
  288.      * 
  289.      * This method may skip over some smaller number of bytes, possibly zero. 
  290.      * This may result from any of a number of conditions; reaching end of 
  291.      * file before <code>n</code> bytes have been skipped is only one 
  292.      * possibility. This method never throws an <code>EOFException</code>. 
  293.      * The actual number of bytes skipped is returned.  If <code>n</code> 
  294.      * is negative, no bytes are skipped.
  295.      *
  296.      * @param      n   the number of bytes to be skipped.
  297.      * @return     the actual number of bytes skipped.
  298.      * @exception  IOException  if an I/O error occurs.
  299.      */
  300.     public int skipBytes(int n) throws IOException {
  301.         long pos;
  302.     long len;
  303.     long newpos; 
  304.  
  305.     if (n <= 0) {
  306.         return 0;
  307.     }
  308.     pos = getFilePointer();
  309.     len = length();
  310.     newpos = pos + n;
  311.     if (newpos > len) {
  312.         newpos = len;
  313.     }
  314.     seek(newpos);
  315.  
  316.     /* return the actual number of bytes skipped */
  317.     return (int) (newpos - pos);
  318.     }
  319.  
  320.     // 'Write' primitives
  321.  
  322.     /**
  323.      * Writes the specified byte to this file. The write starts at 
  324.      * the current file pointer.
  325.      *
  326.      * @param      b   the <code>byte</code> to be written.
  327.      * @exception  IOException  if an I/O error occurs.
  328.      */
  329.     public native void write(int b) throws IOException;
  330.  
  331.     /**
  332.      * Writes a sub array as a sequence of bytes. 
  333.      * @param b the data to be written
  334.  
  335.      * @param off the start offset in the data
  336.      * @param len the number of bytes that are written
  337.      * @exception IOException If an I/O error has occurred.
  338.      */
  339.     private native void writeBytes(byte b[], int off, int len) throws IOException;
  340.  
  341.     /**
  342.      * Writes <code>b.length</code> bytes from the specified byte array 
  343.      * to this file, starting at the current file pointer. 
  344.      *
  345.      * @param      b   the data.
  346.      * @exception  IOException  if an I/O error occurs.
  347.      */
  348.     public void write(byte b[]) throws IOException {
  349.     writeBytes(b, 0, b.length); 
  350.     }
  351.  
  352.     /**
  353.      * Writes <code>len</code> bytes from the specified byte array 
  354.      * starting at offset <code>off</code> to this file. 
  355.      *
  356.      * @param      b     the data.
  357.      * @param      off   the start offset in the data.
  358.      * @param      len   the number of bytes to write.
  359.      * @exception  IOException  if an I/O error occurs.
  360.      */
  361.     public void write(byte b[], int off, int len) throws IOException {
  362.     writeBytes(b, off, len);
  363.     }
  364.  
  365.     // 'Random access' stuff
  366.  
  367.     /**
  368.      * Returns the current offset in this file. 
  369.      *
  370.      * @return     the offset from the beginning of the file, in bytes,
  371.      *             at which the next read or write occurs.
  372.      * @exception  IOException  if an I/O error occurs.
  373.      */
  374.     public native long getFilePointer() throws IOException;
  375.  
  376.     /**
  377.      * Sets the file-pointer offset, measured from the beginning of this 
  378.      * file, at which the next read or write occurs.  The offset may be 
  379.      * set beyond the end of the file. Setting the offset beyond the end 
  380.      * of the file does not change the file length.  The file length will 
  381.      * change only by writing after the offset has been set beyond the end 
  382.      * of the file. 
  383.      *
  384.      * @param      pos   the offset position, measured in bytes from the 
  385.      *                   beginning of the file, at which to set the file 
  386.      *                   pointer.
  387.      * @exception  IOException  if <code>pos</code> is less than 
  388.      *                          <code>0</code> or if an I/O error occurs.
  389.      */
  390.     public native void seek(long pos) throws IOException;
  391.  
  392.     /**
  393.      * Returns the length of this file.
  394.      *
  395.      * @return     the length of this file, measured in bytes.
  396.      * @exception  IOException  if an I/O error occurs.
  397.      */
  398.     public native long length() throws IOException;
  399.  
  400.     /**
  401.      * Sets the length of this file.
  402.      *
  403.      * <p> If the present length of the file as returned by the
  404.      * <code>length</code> method is greater than the <code>newLength</code>
  405.      * argument then the file will be truncated.  In this case, if the file
  406.      * offset as returned by the <code>getFilePointer</code> method is greater
  407.      * then <code>newLength</code> then after this method returns the offset
  408.      * will be equal to <code>newLength</code>.
  409.      *
  410.      * <p> If the present length of the file as returned by the
  411.      * <code>length</code> method is smaller than the <code>newLength</code>
  412.      * argument then the file will be extended.  In this case, the contents of
  413.      * the extended portion of the file are not defined.
  414.      *
  415.      * @param      newLength    The desired length of the file
  416.      * @exception  IOException  If an I/O error occurs
  417.      * @since      JDK1.2
  418.      */
  419.     public native void setLength(long newLength) throws IOException;
  420.  
  421.     /**
  422.      * Closes this random access file stream and releases any system 
  423.      * resources associated with the stream. A closed random access 
  424.      * file cannot perform input or output operations and cannot be 
  425.      * reopened.
  426.      *
  427.      * @exception  IOException  if an I/O error occurs.
  428.      */
  429.     public native void close() throws IOException;
  430.  
  431.     //
  432.     //  Some "reading/writing Java data types" methods stolen from
  433.     //  DataInputStream and DataOutputStream.
  434.     //
  435.  
  436.     /**
  437.      * Reads a <code>boolean</code> from this file. This method reads a 
  438.      * single byte from the file, starting at the current file pointer. 
  439.      * A value of <code>0</code> represents 
  440.      * <code>false</code>. Any other value represents <code>true</code>. 
  441.      * This method blocks until the byte is read, the end of the stream 
  442.      * is detected, or an exception is thrown. 
  443.      *
  444.      * @return     the <code>boolean</code> value read.
  445.      * @exception  EOFException  if this file has reached the end.
  446.      * @exception  IOException   if an I/O error occurs.
  447.      */
  448.     public final boolean readBoolean() throws IOException {
  449.     int ch = this.read();
  450.     if (ch < 0)
  451.         throw new EOFException();
  452.     return (ch != 0);
  453.     }
  454.  
  455.     /**
  456.      * Reads a signed eight-bit value from this file. This method reads a 
  457.      * byte from the file, starting from the current file pointer. 
  458.      * If the byte read is <code>b</code>, where 
  459.      * <code>0 <= b <= 255</code>, 
  460.      * then the result is:
  461.      * <blockquote><pre>
  462.      *     (byte)(b)
  463.      * </pre></blockquote>
  464.      * <p>
  465.      * This method blocks until the byte is read, the end of the stream 
  466.      * is detected, or an exception is thrown. 
  467.      *
  468.      * @return     the next byte of this file as a signed eight-bit
  469.      *             <code>byte</code>.
  470.      * @exception  EOFException  if this file has reached the end.
  471.      * @exception  IOException   if an I/O error occurs.
  472.      */
  473.     public final byte readByte() throws IOException {
  474.     int ch = this.read();
  475.     if (ch < 0)
  476.         throw new EOFException();
  477.     return (byte)(ch);
  478.     }
  479.  
  480.     /**
  481.      * Reads an unsigned eight-bit number from this file. This method reads 
  482.      * a byte from this file, starting at the current file pointer, 
  483.      * and returns that byte. 
  484.      * <p>
  485.      * This method blocks until the byte is read, the end of the stream 
  486.      * is detected, or an exception is thrown. 
  487.      *
  488.      * @return     the next byte of this file, interpreted as an unsigned
  489.      *             eight-bit number.
  490.      * @exception  EOFException  if this file has reached the end.
  491.      * @exception  IOException   if an I/O error occurs.
  492.      */
  493.     public final int readUnsignedByte() throws IOException {
  494.     int ch = this.read();
  495.     if (ch < 0)
  496.         throw new EOFException();
  497.     return ch;
  498.     }
  499.  
  500.     /**
  501.      * Reads a signed 16-bit number from this file. The method reads two 
  502.      * bytes from this file, starting at the current file pointer. 
  503.      * If the two bytes read, in order, are 
  504.      * <code>b1</code> and <code>b2</code>, where each of the two values is 
  505.      * between <code>0</code> and <code>255</code>, inclusive, then the 
  506.      * result is equal to:
  507.      * <blockquote><pre>
  508.      *     (short)((b1 << 8) | b2)
  509.      * </pre></blockquote>
  510.      * <p>
  511.      * This method blocks until the two bytes are read, the end of the 
  512.      * stream is detected, or an exception is thrown. 
  513.      *
  514.      * @return     the next two bytes of this file, interpreted as a signed
  515.      *             16-bit number.
  516.      * @exception  EOFException  if this file reaches the end before reading
  517.      *               two bytes.
  518.      * @exception  IOException   if an I/O error occurs.
  519.      */
  520.     public final short readShort() throws IOException {
  521.     int ch1 = this.read();
  522.     int ch2 = this.read();
  523.     if ((ch1 | ch2) < 0)
  524.         throw new EOFException();
  525.     return (short)((ch1 << 8) + (ch2 << 0));
  526.     }
  527.  
  528.     /**
  529.      * Reads an unsigned 16-bit number from this file. This method reads 
  530.      * two bytes from the file, starting at the current file pointer. 
  531.      * If the bytes read, in order, are 
  532.      * <code>b1</code> and <code>b2</code>, where 
  533.      * <code>0 <= b1, b2 <= 255</code>, 
  534.      * then the result is equal to:
  535.      * <blockquote><pre>
  536.      *     (b1 << 8) | b2
  537.      * </pre></blockquote>
  538.      * <p>
  539.      * This method blocks until the two bytes are read, the end of the 
  540.      * stream is detected, or an exception is thrown. 
  541.      *
  542.      * @return     the next two bytes of this file, interpreted as an unsigned
  543.      *             16-bit integer.
  544.      * @exception  EOFException  if this file reaches the end before reading
  545.      *               two bytes.
  546.      * @exception  IOException   if an I/O error occurs.
  547.      */
  548.     public final int readUnsignedShort() throws IOException {
  549.     int ch1 = this.read();
  550.     int ch2 = this.read();
  551.     if ((ch1 | ch2) < 0)
  552.         throw new EOFException();
  553.     return (ch1 << 8) + (ch2 << 0);
  554.     }
  555.  
  556.     /**
  557.      * Reads a Unicode character from this file. This method reads two
  558.      * bytes from the file, starting at the current file pointer. 
  559.      * If the bytes read, in order, are 
  560.      * <code>b1</code> and <code>b2</code>, where 
  561.      * <code>0 <= b1, b2 <= 255</code>, 
  562.      * then the result is equal to:
  563.      * <blockquote><pre>
  564.      *     (char)((b1 << 8) | b2)
  565.      * </pre></blockquote>
  566.      * <p>
  567.      * This method blocks until the two bytes are read, the end of the 
  568.      * stream is detected, or an exception is thrown. 
  569.      *
  570.      * @return     the next two bytes of this file as a Unicode character.
  571.      * @exception  EOFException  if this file reaches the end before reading
  572.      *               two bytes.
  573.      * @exception  IOException   if an I/O error occurs.
  574.      */
  575.     public final char readChar() throws IOException {
  576.     int ch1 = this.read();
  577.     int ch2 = this.read();
  578.     if ((ch1 | ch2) < 0)
  579.         throw new EOFException();
  580.     return (char)((ch1 << 8) + (ch2 << 0));
  581.     }
  582.  
  583.     /**
  584.      * Reads a signed 32-bit integer from this file. This method reads 4 
  585.      * bytes from the file, starting at the current file pointer. 
  586.      * If the bytes read, in order, are <code>b1</code>,
  587.      * <code>b2</code>, <code>b3</code>, and <code>b4</code>, where 
  588.      * <code>0 <= b1, b2, b3, b4 <= 255</code>, 
  589.      * then the result is equal to:
  590.      * <blockquote><pre>
  591.      *     (b1 << 24) | (b2 << 16) + (b3 << 8) + b4
  592.      * </pre></blockquote>
  593.      * <p>
  594.      * This method blocks until the four bytes are read, the end of the 
  595.      * stream is detected, or an exception is thrown. 
  596.      *
  597.      * @return     the next four bytes of this file, interpreted as an
  598.      *             <code>int</code>.
  599.      * @exception  EOFException  if this file reaches the end before reading
  600.      *               four bytes.
  601.      * @exception  IOException   if an I/O error occurs.
  602.      */
  603.     public final int readInt() throws IOException {
  604.     int ch1 = this.read();
  605.     int ch2 = this.read();
  606.     int ch3 = this.read();
  607.     int ch4 = this.read();
  608.     if ((ch1 | ch2 | ch3 | ch4) < 0)
  609.         throw new EOFException();
  610.     return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
  611.     }
  612.  
  613.     /**
  614.      * Reads a signed 64-bit integer from this file. This method reads eight
  615.      * bytes from the file, starting at the current file pointer. 
  616.      * If the bytes read, in order, are 
  617.      * <code>b1</code>, <code>b2</code>, <code>b3</code>, 
  618.      * <code>b4</code>, <code>b5</code>, <code>b6</code>, 
  619.      * <code>b7</code>, and <code>b8,</code> where:
  620.      * <blockquote><pre>
  621.      *     0 <= b1, b2, b3, b4, b5, b6, b7, b8 <=255,
  622.      * </pre></blockquote>
  623.      * <p>
  624.      * then the result is equal to:
  625.      * <p><blockquote><pre>
  626.      *     ((long)b1 << 56) + ((long)b2 << 48)
  627.      *     + ((long)b3 << 40) + ((long)b4 << 32)
  628.      *     + ((long)b5 << 24) + ((long)b6 << 16)
  629.      *     + ((long)b7 << 8) + b8
  630.      * </pre></blockquote>
  631.      * <p>
  632.      * This method blocks until the eight bytes are read, the end of the 
  633.      * stream is detected, or an exception is thrown. 
  634.      *
  635.      * @return     the next eight bytes of this file, interpreted as a
  636.      *             <code>long</code>.
  637.      * @exception  EOFException  if this file reaches the end before reading
  638.      *               eight bytes.
  639.      * @exception  IOException   if an I/O error occurs.
  640.      */
  641.     public final long readLong() throws IOException {
  642.     return ((long)(readInt()) << 32) + (readInt() & 0xFFFFFFFFL);
  643.     }
  644.  
  645.     /**
  646.      * Reads a <code>float</code> from this file. This method reads an 
  647.      * <code>int</code> value, starting at the current file pointer, 
  648.      * as if by the <code>readInt</code> method 
  649.      * and then converts that <code>int</code> to a <code>float</code> 
  650.      * using the <code>intBitsToFloat</code> method in class 
  651.      * <code>Float</code>. 
  652.      * <p>
  653.      * This method blocks until the four bytes are read, the end of the 
  654.      * stream is detected, or an exception is thrown. 
  655.      *
  656.      * @return     the next four bytes of this file, interpreted as a
  657.      *             <code>float</code>.
  658.      * @exception  EOFException  if this file reaches the end before reading
  659.      *             four bytes.
  660.      * @exception  IOException   if an I/O error occurs.
  661.      * @see        java.io.RandomAccessFile#readInt()
  662.      * @see        java.lang.Float#intBitsToFloat(int)
  663.      */
  664.     public final float readFloat() throws IOException {
  665.     return Float.intBitsToFloat(readInt());
  666.     }
  667.  
  668.     /**
  669.      * Reads a <code>double</code> from this file. This method reads a 
  670.      * <code>long</code> value, starting at the current file pointer, 
  671.      * as if by the <code>readLong</code> method 
  672.      * and then converts that <code>long</code> to a <code>double</code> 
  673.      * using the <code>longBitsToDouble</code> method in 
  674.      * class <code>Double</code>.
  675.      * <p>
  676.      * This method blocks until the eight bytes are read, the end of the 
  677.      * stream is detected, or an exception is thrown. 
  678.      *
  679.      * @return     the next eight bytes of this file, interpreted as a
  680.      *             <code>double</code>.
  681.      * @exception  EOFException  if this file reaches the end before reading
  682.      *             eight bytes.
  683.      * @exception  IOException   if an I/O error occurs.
  684.      * @see        java.io.RandomAccessFile#readLong()
  685.      * @see        java.lang.Double#longBitsToDouble(long)
  686.      */
  687.     public final double readDouble() throws IOException {
  688.     return Double.longBitsToDouble(readLong());
  689.     }
  690.  
  691.     /**
  692.      * Reads the next line of text from this file.  This method successively
  693.      * reads bytes from the file, starting at the current file pointer, 
  694.      * until it reaches a line terminator or the end
  695.      * of the file.  Each byte is converted into a character by taking the
  696.      * byte's value for the lower eight bits of the character and setting the
  697.      * high eight bits of the character to zero.  This method does not,
  698.      * therefore, support the full Unicode character set.
  699.      *
  700.      * <p> A line of text is terminated by a carriage-return character
  701.      * (<code>'\r'</code>), a newline character (<code>'\n'</code>), a
  702.      * carriage-return character immediately followed by a newline character,
  703.      * or the end of the file.  Line-terminating characters are discarded and
  704.      * are not included as part of the string returned.
  705.      *
  706.      * <p> This method blocks until a newline character is read, a carriage
  707.      * return and the byte following it are read (to see if it is a newline),
  708.      * the end of the file is reached, or an exception is thrown.
  709.      *
  710.      * @return     the next line of text from this file, or null if end
  711.      *             of file is encountered before even one byte is read.
  712.      * @exception  IOException  if an I/O error occurs.
  713.      */
  714.  
  715.     public final String readLine() throws IOException {
  716.     StringBuffer input = new StringBuffer();
  717.     int c = -1;
  718.     boolean eol = false;
  719.  
  720.     while (!eol) {
  721.         switch (c = read()) {
  722.         case -1:
  723.         case '\n':
  724.         eol = true;
  725.         break;
  726.         case '\r':
  727.         eol = true;
  728.         long cur = getFilePointer();
  729.         if ((read()) != '\n') {
  730.             seek(cur);
  731.         }
  732.         break;
  733.         default:
  734.         input.append((char)c);
  735.         break;
  736.         }
  737.     }
  738.  
  739.     if ((c == -1) && (input.length() == 0)) {
  740.         return null;
  741.     }
  742.     return input.toString();
  743.     }
  744.  
  745.     /**
  746.      * Reads in a string from this file. The string has been encoded 
  747.      * using a modified UTF-8 format. 
  748.      * <p>
  749.      * The first two bytes are read, starting from the current file 
  750.      * pointer, as if by 
  751.      * <code>readUnsignedShort</code>. This value gives the number of 
  752.      * following bytes that are in the encoded string, not
  753.      * the length of the resulting string. The following bytes are then 
  754.      * interpreted as bytes encoding characters in the UTF-8 format 
  755.      * and are converted into characters. 
  756.      * <p>
  757.      * This method blocks until all the bytes are read, the end of the 
  758.      * stream is detected, or an exception is thrown. 
  759.      *
  760.      * @return     a Unicode string.
  761.      * @exception  EOFException            if this file reaches the end before
  762.      *               reading all the bytes.
  763.      * @exception  IOException             if an I/O error occurs.
  764.      * @exception  UTFDataFormatException  if the bytes do not represent 
  765.      *               valid UTF-8 encoding of a Unicode string.
  766.      * @see        java.io.RandomAccessFile#readUnsignedShort()
  767.      */
  768.     public final String readUTF() throws IOException {
  769.     return DataInputStream.readUTF(this);
  770.     }
  771.  
  772.     /**
  773.      * Writes a <code>boolean</code> to the file as a one-byte value. The 
  774.      * value <code>true</code> is written out as the value 
  775.      * <code>(byte)1</code>; the value <code>false</code> is written out 
  776.      * as the value <code>(byte)0</code>. The write starts at 
  777.      * the current position of the file pointer.
  778.      *
  779.      * @param      v   a <code>boolean</code> value to be written.
  780.      * @exception  IOException  if an I/O error occurs.
  781.      */
  782.     public final void writeBoolean(boolean v) throws IOException {
  783.     write(v ? 1 : 0);
  784.     //written++;
  785.     }
  786.  
  787.     /**
  788.      * Writes a <code>byte</code> to the file as a one-byte value. The 
  789.      * write starts at the current position of the file pointer.
  790.      *
  791.      * @param      v   a <code>byte</code> value to be written.
  792.      * @exception  IOException  if an I/O error occurs.
  793.      */
  794.     public final void writeByte(int v) throws IOException {
  795.     write(v);
  796.     //written++;
  797.     }
  798.  
  799.     /**
  800.      * Writes a <code>short</code> to the file as two bytes, high byte first. 
  801.      * The write starts at the current position of the file pointer.
  802.      *
  803.      * @param      v   a <code>short</code> to be written.
  804.      * @exception  IOException  if an I/O error occurs.
  805.      */
  806.     public final void writeShort(int v) throws IOException {
  807.     write((v >>> 8) & 0xFF);
  808.     write((v >>> 0) & 0xFF);
  809.     //written += 2;
  810.     }
  811.  
  812.     /**
  813.      * Writes a <code>char</code> to the file as a two-byte value, high
  814.      * byte first. The write starts at the current position of the 
  815.      * file pointer.
  816.      *
  817.      * @param      v   a <code>char</code> value to be written.
  818.      * @exception  IOException  if an I/O error occurs.
  819.      */
  820.     public final void writeChar(int v) throws IOException {
  821.     write((v >>> 8) & 0xFF);
  822.     write((v >>> 0) & 0xFF);
  823.     //written += 2;
  824.     }
  825.  
  826.     /**
  827.      * Writes an <code>int</code> to the file as four bytes, high byte first. 
  828.      * The write starts at the current position of the file pointer.
  829.      *
  830.      * @param      v   an <code>int</code> to be written.
  831.      * @exception  IOException  if an I/O error occurs.
  832.      */
  833.     public final void writeInt(int v) throws IOException {
  834.     write((v >>> 24) & 0xFF);
  835.     write((v >>> 16) & 0xFF);
  836.     write((v >>>  8) & 0xFF);
  837.     write((v >>>  0) & 0xFF);
  838.     //written += 4;
  839.     }
  840.  
  841.     /**
  842.      * Writes a <code>long</code> to the file as eight bytes, high byte first. 
  843.      * The write starts at the current position of the file pointer.
  844.      *
  845.      * @param      v   a <code>long</code> to be written.
  846.      * @exception  IOException  if an I/O error occurs.
  847.      */
  848.     public final void writeLong(long v) throws IOException {
  849.     write((int)(v >>> 56) & 0xFF);
  850.     write((int)(v >>> 48) & 0xFF);
  851.     write((int)(v >>> 40) & 0xFF);
  852.     write((int)(v >>> 32) & 0xFF);
  853.     write((int)(v >>> 24) & 0xFF);
  854.     write((int)(v >>> 16) & 0xFF);
  855.     write((int)(v >>>  8) & 0xFF);
  856.     write((int)(v >>>  0) & 0xFF);
  857.     //written += 8;
  858.     }
  859.  
  860.     /**
  861.      * Converts the float argument to an <code>int</code> using the 
  862.      * <code>floatToIntBits</code> method in class <code>Float</code>, 
  863.      * and then writes that <code>int</code> value to the file as a 
  864.      * four-byte quantity, high byte first. The write starts at the 
  865.      * current position of the file pointer.
  866.      *
  867.      * @param      v   a <code>float</code> value to be written.
  868.      * @exception  IOException  if an I/O error occurs.
  869.      * @see        java.lang.Float#floatToIntBits(float)
  870.      */
  871.     public final void writeFloat(float v) throws IOException {
  872.     writeInt(Float.floatToIntBits(v));
  873.     }
  874.  
  875.     /**
  876.      * Converts the double argument to a <code>long</code> using the 
  877.      * <code>doubleToLongBits</code> method in class <code>Double</code>, 
  878.      * and then writes that <code>long</code> value to the file as an 
  879.      * eight-byte quantity, high byte first. The write starts at the current 
  880.      * position of the file pointer.
  881.      *
  882.      * @param      v   a <code>double</code> value to be written.
  883.      * @exception  IOException  if an I/O error occurs.
  884.      * @see        java.lang.Double#doubleToLongBits(double)
  885.      */
  886.     public final void writeDouble(double v) throws IOException {
  887.     writeLong(Double.doubleToLongBits(v));
  888.     }
  889.  
  890.     /**
  891.      * Writes the string to the file as a sequence of bytes. Each 
  892.      * character in the string is written out, in sequence, by discarding 
  893.      * its high eight bits. The write starts at the current position of 
  894.      * the file pointer.
  895.      *
  896.      * @param      s   a string of bytes to be written.
  897.      * @exception  IOException  if an I/O error occurs.
  898.      */
  899.     public final void writeBytes(String s) throws IOException {
  900.     int len = s.length();
  901.     byte[] b = new byte[len];
  902.     s.getBytes(0, len, b, 0);
  903.     writeBytes(b, 0, len);
  904.     }
  905.  
  906.     /**
  907.      * Writes a string to the file as a sequence of characters. Each 
  908.      * character is written to the data output stream as if by the 
  909.      * <code>writeChar</code> method. The write starts at the current 
  910.      * position of the file pointer.
  911.      *
  912.      * @param      s   a <code>String</code> value to be written.
  913.      * @exception  IOException  if an I/O error occurs.
  914.      * @see        java.io.RandomAccessFile#writeChar(int)
  915.      */
  916.     public final void writeChars(String s) throws IOException {
  917.     int clen = s.length();
  918.     int blen = 2*clen;
  919.     byte[] b = new byte[blen];
  920.     char[] c = new char[clen];
  921.     s.getChars(0, clen, c, 0);
  922.     for (int i = 0, j = 0; i < clen; i++) {
  923.         b[j++] = (byte)(c[i] >>> 8);
  924.         b[j++] = (byte)(c[i] >>> 0);
  925.     }
  926.     writeBytes(b, 0, blen);
  927.     }
  928.  
  929.     /**
  930.      * Writes a string to the file using UTF-8 encoding in a 
  931.      * machine-independent manner. 
  932.      * <p>
  933.      * First, two bytes are written to the file, starting at the 
  934.      * current file pointer, as if by the 
  935.      * <code>writeShort</code> method giving the number of bytes to 
  936.      * follow. This value is the number of bytes actually written out, 
  937.      * not the length of the string. Following the length, each character 
  938.      * of the string is output, in sequence, using the UTF-8 encoding 
  939.      * for each character. 
  940.      *
  941.      * @param      str   a string to be written.
  942.      * @exception  IOException  if an I/O error occurs.
  943.      */
  944.     public final void writeUTF(String str) throws IOException {
  945.     int strlen = str.length();
  946.     int utflen = 0;
  947.  
  948.     for (int i = 0 ; i < strlen ; i++) {
  949.         int c = str.charAt(i);
  950.         if ((c >= 0x0001) && (c <= 0x007F)) {
  951.         utflen++;
  952.         } else if (c > 0x07FF) {
  953.         utflen += 3;
  954.         } else {
  955.         utflen += 2;
  956.         }
  957.     }
  958.  
  959.     if (utflen > 65535)
  960.         throw new UTFDataFormatException();          
  961.  
  962.     write((utflen >>> 8) & 0xFF);
  963.     write((utflen >>> 0) & 0xFF);
  964.     for (int i = 0 ; i < strlen ; i++) {
  965.         int c = str.charAt(i);
  966.         if ((c >= 0x0001) && (c <= 0x007F)) {
  967.         write(c);
  968.         } else if (c > 0x07FF) {
  969.         write(0xE0 | ((c >> 12) & 0x0F));
  970.         write(0x80 | ((c >>  6) & 0x3F));
  971.         write(0x80 | ((c >>  0) & 0x3F));
  972.         //written += 2;
  973.         } else {
  974.         write(0xC0 | ((c >>  6) & 0x1F));
  975.         write(0x80 | ((c >>  0) & 0x3F));
  976.         //written += 1;
  977.         }
  978.     }
  979.     //written += strlen + 2;
  980.     }
  981.  
  982.     private static native void initIDs();
  983.  
  984.     static {
  985.     initIDs();
  986.     }
  987.  
  988. }
  989.